Create a range for floating numbersΒΆ
Create a range for floating numbers.
Expected output:
[0.0,
0.1,
0.2,
0.30000000000000004,
0.4,
0.5,
0.6000000000000001,
0.7000000000000001,
0.8,
0.9,
1.0
]
def frange(x, y, jump=1.0):
i = 0.0
x = float(x) # Prevent yielding integers.
y = float(y) # Comparison converts y to float every time otherwise.
x0 = x
epsilon = jump / 2.0
yield x # yield always first value
while x + epsilon < y:
i += 1.0
x = x0 + i * jump
yield x
# test
print(list(frange(0.0, 1.0, 0.1)))
Output:
[0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9, 1.0]